package com.prs.crm.action.cqunicom;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.domain.cqunicom.AudOrderCount;
import com.prs.crm.domain.cqunicom.MonitorOrder;
import com.prs.crm.domain.cqunicom.PoplarOrder;
import com.prs.crm.domain.cqunicom.Services;
import com.prs.crm.domain.cqunicom.ServicesType;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.hr.Role;
import com.prs.crm.service.cqunicom.BusinessOrderService;
import com.prs.crm.service.cqunicom.CompanyService;
import com.prs.crm.service.cqunicom.MonitorOrderService;
import com.prs.crm.service.cqunicom.OrderService;
import com.prs.crm.service.cqunicom.ServicesService;
import com.prs.crm.service.cqunicom.ServicesTypeService;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.hr.RoleService;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2011-3-15 下午06:28:52 简单说明 评估统计Action
 */

public class AssessmentAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private PaginationSupport pagination;

	private BusinessOrderService businessOrderService;

	private MonitorOrder monitorOrder;

	private MonitorOrderService monitorOrderService;

	private CompanyService companyService;

	private PoplarOrder order;

	private OrderService orderService;
	
	private Role role;

	private RoleService roleService;
	
	private Person person;
	
	private PersonService personService;

	private Services services;
	
	private String models="";

	private ServicesService servicesService;

	private ServicesType servicesType;

	private ServicesTypeService servicesTypeService;

	private String rowdata;
	
	private String roleStyle;

	private String timeSel="";
	
	private Date startTimes=null;
	
	private Date endTimes=null;
	
	private Integer perObject;
	
	private String jiaose="";
	
	private String starts="";
	
	private String ends="";
	
	private AudOrderCount  auObject=new AudOrderCount();
	
	@Action("listAssessmentCount")
	public String listAssessmentCount() {
		
		 SimpleDateFormat ddf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");		 
		  if(startTimes==null){
			  Calendar c = Calendar.getInstance();
			  c.add(Calendar.DAY_OF_MONTH, -7);
			  Date _currt=c.getTime();
			  starts=ddf.format(_currt);//开始时间  
		  }else{
			  starts=ddf.format(startTimes); 
		  }
		  if(endTimes==null){
			 ends=ddf.format(new Date());//结束时间 
		  }else{
			  ends=ddf.format(endTimes);
		  }
		  List<PoplarOrder>  zjList=this.getOrderService().getTraferOrder();
		 //总的派单查找
		  List<PoplarOrder>  pdList=this.getOrderService().getDispatchOrder();
		 //应回访数
		  int hfNum=0;
		  //实际回访数
		  int sjNum=0;
		  //回访率
		  //无效回访率
		  int wxhf=0;

		  //客户满意度
		  int mayidu=0;

		  for(PoplarOrder  po:pdList){
			  if(po.getBusinessOrder()!=null){
				  hfNum++;
				  if(po.getMonitorOrder()!=null){
					  sjNum++;	
					  if("满意".equals(po.getMonitorOrder().getEnjoyDegree())){
						  mayidu++;  
					  }
					  if("无效回访".equals(po.getMonitorOrder().getVisitState())){
						  wxhf++;
					  }
				  }
			  }
			  
		  }
		    auObject.setPdNum(pdList.size()+"");
			auObject.setyNum(hfNum+"");
			auObject.setSjNum(sjNum+"");
			if(hfNum==0){
				auObject.setHflv("0%");
			}else{
				auObject.setHflv((sjNum/hfNum*100)+"%");
			}
			if(sjNum==0){
				auObject.setWxhflv("0%");
			}else{
				auObject.setWxhflv(wxhf/sjNum*100+"%");
			}
			auObject.setKhmyd(mayidu+"");
			
			auObject.setHj((pdList.size()+zjList.size())+"");
			auObject.setPdsl(pdList.size()+"");
			auObject.setZjsl(zjList.size()+"");
			auObject.setJiaose(jiaose);
			
			//--------------/////fenkai 
		if("".equals(timeSel) || timeSel ==null){
			DetachedCriteria criteria = this.getPaginationHelper().createQuery(PoplarOrder.class);
			criteria.add(Restrictions.eq("modleName", "派单"));
			pagination = this.getPaginationHelper().prepare(pagination);
			this.setPagination(this.getOrderService().listByPage(criteria,pagination));
			
			
		}else{
			
			DetachedCriteria criteria = this.getPaginationHelper().createQuery(PoplarOrder.class);
			
			if("派单时间".equals(timeSel)){
			criteria.add(Restrictions.between("orderDate",startTimes,endTimes));
			}else if("完成时间".equals(timeSel)){
			criteria.add(Restrictions.between("compleDate",startTimes,endTimes));
			}
			
         
            Person per=this.getPersonService().get(perObject);
          
            if("A".equals(jiaose)){//班长
            	if(perObject==0){
            		
            	}else{
            	if(per!=null){
            	criteria.add(Restrictions.eq("person", per));
            	}else{
            	criteria.add(Restrictions.eq("id", 0));	
            	}
            	}
            }
            else if("B".equals(jiaose)){//坐席
            	if(perObject==0){
            		
            	}else{
            		if(per!=null){
                		criteria.createAlias("monitorOrder", "cn");
            			criteria.add(Restrictions.eq("cn.personTeam", per));
            		}else{
            			criteria.add(Restrictions.eq("id", 0));
            		}
            	}
            	
            }
        
			if("转接".equals(models)){
				criteria.add(Restrictions.eq("modleName", "转接"));
			}else{
				criteria.add(Restrictions.eq("modleName", "派单"));
			}
			
			
			
			pagination = this.getPaginationHelper().prepare(pagination);
			this.setPagination(this.getOrderService().listByPage(criteria,pagination));
		}
		
		
		
		
		
		
		
		

		return SUCCESS;
	}

	@Action(value = "SerchRole", results = { @Result(name = "success", type = "json", params = {
			"root", "rowdata" }) })
	public String SerchRole() {
	
		String rowdatas="";
		if("A".equals(roleStyle)){
			//班长
		Role roleObject=this.getRoleService().getByNameRole("班长");
		List<Person> dlist=this.getPersonService().getNoticePerson(roleObject);
		for(Person p:dlist){
			if("".equals(rowdatas)){
				rowdatas=p.getId()+"-"+p.getUserName();	
			}else{
				rowdatas=rowdatas+","+p.getId()+"-"+p.getUserName();	
			}
		}
		}else if("B".equals(roleStyle)){
			//坐席
			Role roleObject=this.getRoleService().getByNameRole("坐席");
			List<Person> dlist=this.getPersonService().getNoticePerson(roleObject);
			for(Person p:dlist){
				if("".equals(rowdatas)){
					rowdatas=p.getId()+"-"+p.getUserName();	
				}else{
					rowdatas=rowdatas+","+p.getId()+"-"+p.getUserName();	
				}
			}
		}
		this.rowdata=rowdatas;
		return SUCCESS;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public BusinessOrderService getBusinessOrderService() {
		return businessOrderService;
	}

	public String getJiaose() {
		return jiaose;
	}

	public void setJiaose(String jiaose) {
		this.jiaose = jiaose;
	}

	public void setBusinessOrderService(
			BusinessOrderService businessOrderService) {
		this.businessOrderService = businessOrderService;
	}
	public String getTimeSel() {
		return timeSel;
	}

	public void setTimeSel(String timeSel) {
		this.timeSel = timeSel;
	}

	public Date getStartTimes() {
		return startTimes;
	}

	public void setStartTimes(Date startTimes) {
		this.startTimes = startTimes;
	}

	public Date getEndTimes() {
		return endTimes;
	}

	public void setEndTimes(Date endTimes) {
		this.endTimes = endTimes;
	}

	public Integer getPerObject() {
		return perObject;
	}

	public String getModels() {
		return models;
	}

	public void setModels(String models) {
		this.models = models;
	}

	public void setPerObject(Integer perObject) {
		this.perObject = perObject;
	}

	public AudOrderCount getAuObject() {
		return auObject;
	}

	public void setAuObject(AudOrderCount auObject) {
		this.auObject = auObject;
	}

	public String getRowdata() {
		return rowdata;
	}

	public void setRowdata(String rowdata) {
		this.rowdata = rowdata;
	}

	public MonitorOrder getMonitorOrder() {
		return monitorOrder;
	}

	public void setMonitorOrder(MonitorOrder monitorOrder) {
		this.monitorOrder = monitorOrder;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public MonitorOrderService getMonitorOrderService() {
		return monitorOrderService;
	}

	public void setMonitorOrderService(MonitorOrderService monitorOrderService) {
		this.monitorOrderService = monitorOrderService;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public PoplarOrder getOrder() {
		return order;
	}

	public void setOrder(PoplarOrder order) {
		this.order = order;
	}

	public OrderService getOrderService() {
		return orderService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public Services getServices() {
		return services;
	}

	public void setServices(Services services) {
		this.services = services;
	}

	public ServicesService getServicesService() {
		return servicesService;
	}

	public void setServicesService(ServicesService servicesService) {
		this.servicesService = servicesService;
	}

	public ServicesType getServicesType() {
		return servicesType;
	}

	public void setServicesType(ServicesType servicesType) {
		this.servicesType = servicesType;
	}

	public ServicesTypeService getServicesTypeService() {
		return servicesTypeService;
	}

	public void setServicesTypeService(ServicesTypeService servicesTypeService) {
		this.servicesTypeService = servicesTypeService;
	}

	public String getRoleStyle() {
		return roleStyle;
	}

	public void setRoleStyle(String roleStyle) {
		this.roleStyle = roleStyle;
	}

	public String getStarts() {
		return starts;
	}

	public void setStarts(String starts) {
		this.starts = starts;
	}

	public String getEnds() {
		return ends;
	}

	public void setEnds(String ends) {
		this.ends = ends;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

}
